<!--********************************************************************
* Copyright© 2000 - 2018 SuperMap Software Co.Ltd. All rights reserved.
*********************************************************************-->
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=Edge">
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width">
    <title data-i18n="resources.title_mb_graphicLayer"></title>
    <style>
        body {
            margin: 0;
            overflow: hidden;
            background: #fff;
            width: 100%;
            height: 100%
        }

        #map {
            position: absolute;
            top: 0;
            bottom: 0;
            width: 100%;
        }

        #title {
            position: absolute;
            color: white;
            left: 0;
            top: 30px;
            text-align: center;
            width: 100%;
            z-index: 2;
        }
        #title > h3{
            margin: 10px 0;
            letter-spacing: 0.1em;
        }
        #title > h6 {
            margin: 0;
            font-weight: normal;
        }
    </style>
</head>
<body>
<div id="title">
    <h3 data-i18n="resources.text_graphicLayer_title"></h3>
    <h6 data-i18n="resources.text_graphicLayer_subTitle"></h6>
</div>
<div id="map"></div>
<script type="text/javascript" include="papaparse,jquery,dat-gui,widgets" src="../js/include-web.js"></script>
<script type="text/javascript" include="deck" src="../../dist/mapboxgl/include-mapboxgl.js"></script>
<script type="text/javascript">

    var host = window.isLocal ? window.server : "http://support.supermap.com.cn:8090",
        url = host + "/iserver/services/map-china400/rest/maps/ChinaDark";
    var map, graphicLayer;

    var attribution = "<a href='https://www.mapbox.com/about/maps/' target='_blank'>© Mapbox </a>" +
            " with <span>© <a href='http://iclient.supermap.io' target='_blank'>SuperMap iClient</a> | </span>" +
            " Map Data <span>© <a href='http://support.supermap.com.cn/product/iServer.aspx' target='_blank'>SuperMap iServer</a></span> ";
    map = new mapboxgl.Map({
        container: 'map',
        style: {
            "version": 8,
            "sources": {
                "raster-tiles": {
                    "attribution": attribution,
                    "type": "raster",
                    "tiles": [url + '/zxyTileImage.png?z={z}&x={x}&y={y}'],
                    "tileSize": 256,
                },
            },
            "layers": [{
                "id": "simple-tiles",
                "type": "raster",
                "source": "raster-tiles",
                "minzoom": 0,
                "maxzoom": 22
            }]
        },
        center: [-73.91426, 40.7594],
        zoom: 10.64
    });
    map.addControl(new mapboxgl.NavigationControl(), 'top-left');

    widgets.loader.showLoader("data loading...");

    $.get('../data/nyc-taxi.csv', function (csvstr) {
        widgets.loader.removeLoader();
        var result = Papa.parse(csvstr, {skipEmptyLines: true, header: true});
        addLayer(result.data);
    });


    function addLayer(points) {
        var graphics = [], popup = new mapboxgl.Popup({closeOnClick: false}).addTo(map);
        for (var i = 0; i < points.length; i++) {

            var lngLat = {
                lng: parseFloat(points[i].lng),
                lat: parseFloat(points[i].lat)
            };
            /**
             * 可以单独给要素设置颜色和半径:
             *  new mapboxgl.supermap.Graphic(lngLat,{
             *      color:[255,0,0],
             *      radius:40
             *  });
             */

            graphics.push(new mapboxgl.supermap.Graphic(lngLat));
        }

        var graphicStyle = {
            color: [0, 255, 128],
            radius: 10
        };

        graphicLayer = new mapboxgl.supermap.GraphicLayer("graphic", {
            graphics: graphics,
            radius: graphicStyle.radius,
            color: graphicStyle.color,
            highlightColor: [255, 0, 0, 255],
            onClick: function (e) {
                if (!popup.isOpen()) {
                    popup.addTo(map);
                }
                popup.setLngLat(e.lngLat)
                    .setHTML("position:" + JSON.stringify(e.lngLat))

            }
        });

        map.addLayer(graphicLayer);

        initDatGui(graphicStyle)
    }

    //设置面板
    function initDatGui(options) {
        var gui = new dat.GUI();
        var Control = createConfigControl();
        map.addControl(new Control(gui.domElement), 'top-right');

        gui.addColor(options, 'color').onChange(finished);
        gui.add(options, 'radius', 0, 100).onChange(finished);

        function finished() {
            graphicLayer.setStyle(options);
        }

    }

    //创建图层操作控件
    function createConfigControl() {
        function ConfigControl(domElement) {
            this.dom = domElement;
        }

        ConfigControl.prototype.onAdd = function (map) {
            this._map = map;
            this._container = this.dom;
            this._container.className = 'mapboxgl-ctrl ' + this._container.className;
            return this._container;
        };

        ConfigControl.prototype.onRemove = function () {
            this._container.parentNode.removeChild(this._container);
            this._map = undefined;
        };
        return ConfigControl;
    }
</script>
</body>
</html>
